﻿using Finbuckle.MultiTenant;
using Finbuckle.MultiTenant.Abstractions;

namespace MultiTenant.ShareDb.Api.Extensions;  
  
public static class MigrationExtensions   
{  
    public static async Task ApplyMigrateAsync(this IApplicationBuilder app)  
    {  
        using var scope = app.ApplicationServices.CreateScope();
        var store = scope.ServiceProvider.GetRequiredService<IMultiTenantStore<TenantInfo>>();
        var tenants = await store.GetAllAsync();
        if (tenants.Any())
        {
            return;
        }
        await store.TryAddAsync(new TenantInfo
        {
            Id = Guid.NewGuid().ToString(),
            Identifier = "tenant01",
            Name = "Tenant 01",
        });
        await store.TryAddAsync(new TenantInfo
        {
            Id = Guid.NewGuid().ToString(),
            Identifier = "tenant02",
            Name = "Tenant 2",
        });
    }  
}
